In this article, I’m explaining how to generate pdf document in node.js. This article is based on the pdfkit module.
PDFKit is pdf document generation library in node.js that make creating complex, multi-page, printable documents easy. It is written in pure CoffeeScript, but you can choose to use the API in plain ‘ol JavaScript if you like.
In this article create only the simple pdf document by the help of express project and PDFKit module.
There are following steps to create generate pdf in node.js:
Step 1 Create an express project
It is not necessary to create but create a simple demo that which understand the example. So create an express project by express.
Now install all dependency by the npm install command.
Step 2 Install PDFKit
How to install PDFKit for pdf generation it is necessary. Here I simply install by the npm install pdfkit command in command prompt:
This install the pdfkit api and now need to use it.
Step 3- Create an User Interface
Here, create a simple form that get the file name and text content for pdf generation. Let’s create an form in /view/index.jade file:
extends layout
block content
h1= title
p Welcome to #{title}
form(method="post", action="/pdfGenrator")
label(class="label-text") File Name
br
input(type="text", name="filename", placeholder="File Name", class="text-style", maxlength)
br
label(class="label-text") Text
br
textarea(name="content", class="text-style", placeholder="Write something for convert it to pdf")
br
input(type="submit", value="Create PDF", class="button-style")
In this form form action give as pdfGenrator and method is post. This form get the simply file and text content and create the pdf that text on that file name.
Step 5 Create PDF File
This section must important. In this section create a route inn index.js file in the route folder this works when form is submitted. First create the in /routes/index.js. Now add route /pdfGenerator:
router.post('/pdfGenrator',function(req,res){
var PDFDocument = require('pdfkit'); // add pdfkit module to access it
var doc = new PDFDocument(); // create instance of PDFDocument var path=require('path'); // add path module to get path var filename=req.body.filename; // this file name is get by form var content=req.body.content; // this text content is get by form doc.y = 320; // this set the document horizontal position doc.text(content,100,100); doc.write(path.resolve(".")+'/PDF/'+filename+'.pdf'); // it create a file that write the document res.download(path.resolve(".")+'/PDF/'+filename+'.pdf'); // it download this file doc.end(); // document end by the end method });
This section create a pdf document in the base of form. And download that file that file after saving it.
Note: Before run you should create a PDF folder in the application otherwise it give error.
Step 6 Run Application
Let’s ready to run it:
Let’s check it:
Let’s see that when click on the Create PDF button then it automatically download it:
Let’s open this file and check it:
This is successfully create a pdf file.
I hope that this article is helpful for you. Thanks!
Anonymous User
06-Feb-2019Nice Article.
ivy ho
30-Dec-20141. route inn index.js file in =>do you mean route in index.js?
2. First create the in /routes/index.js ??
3. minor grammar fixes
it create ==? it creates or Just use Create
it download==> it downloads or Just use Download
router.post('/pdfGenrator',function(req,res){
/pdfGenrator--> /pdfGenerator ?
4. After you click on the Create PDF button , you will be prompted to download the pdf file
Let’s see that when click on the Create PDF button then it automatically download it:
5. This section must important==> most
6. It will be helpful if you add more details as how to run your application for new user.
Not trying to be picky :-) But hope this will help too.